<?php

namespace app\admin\controller;

use app\common\Helper;
use app\admin\model\Withdrawconfig as WithdrawconfigMdl;

use app\lib\exception\ParameterException;
use think\Db;
use think\Exception;
use think\Request;

/**
 * 提现设置
 */
class Withdrawconfig extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new WithdrawconfigMdl();
    }

    /**
     * 详情
     */
    public function detail(){
        $param = self::getHttpParam();
        $model = $this->model;

        $where = [

        ];
        $fields = '*';
        $item = $model->where($where)->field($fields)->find();
        if(!$item){
            $item = [];
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

    /**
     * 保存设置
     */
    public function saveSet(Request $request){
        $param = $request->param();
        try{
            $this->chckParam($param);
        }catch (Exception $ex){
            return show(config('status.ERROR_STATUS'), $ex->msg, $ex->msg);
        }
        $model = $this->model;

        if (!empty($param['id'])) {
            // 修改
            $id = $param['id'];
            $param['updateDate'] = date('Y-m-d H:i:s');
            $param['updateOper'] = $this->admin->id;
            $result = $model->allowField(true)->save($param, ['id' => $id]);

            // Query执行后的操作
            $model->_after_update($param);
        } else {
            // 新增
            $param['id'] = Helper::getUUID();
            $param['createDate'] = date('Y-m-d H:i:s');
            $param['createOper'] = $this->admin->id;
            $result = $model->save($param);

            // Query执行后的操作
            $model->_after_insert($param);
        }

        if ($result !== false) {
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '保存成功');
        } else {
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
        }
    }

    private function chckParam($param){
        if (empty($param['settleTime']) || (int)$param['settleTime']<0) {
            throw new ParameterException([
                'msg' => '提现次数不合法'
            ]);

        }
        if(empty($param['settleMoneyMin']) || !$this->isCurrency($param['settleMoneyMin'])){
            throw new ParameterException([
                'msg' => '最低提现金额须大于1'
            ]);
        }
        if (empty($param['settleMoneyLimit']) || !$this->isCurrency($param['settleMoneyLimit'])){
            throw new ParameterException([
                'msg' => '最高提现金额须大于1'
            ]);
        }
        if (empty($param['royaltyRate']) || $param['royaltyRate']<0){
            throw new ParameterException([
                'msg' => '提成比例不合法'
            ]);
        }
        if($param['settleMoneyMin']>=$param['settleMoneyLimit']){
            throw new ParameterException([
                'msg' => '最低提现金额必须小于最高提现金额'
            ]);
        }
    }
    private function isCurrency($value){
        //$value精确到分
        if($value<100){
            return false;
        }
        if (preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $value)) {
            return true;
        }else{
            return false;
        }
    }

}